package light.head.controller.oa;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.oa.ApplyInfoDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.MD5Utils;
import light.head.model.ApplyInfo;
import light.head.model.ApplyItem;
import light.head.model.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = Url.GOODS_APPLY)
public class GoodsApplyController extends SupporterController{
	private ApplyInfoDAO aDAO;
	private OrganDAO oDAO;
	private UserInfoDAO userDAO;
	
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.GOODS_APPLY_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.DELETED, Parm.TIME1, Parm.TITLE, Parm.TIME2, Parm.STATUS);
		
		// 获取根机构配置的物品申请流程节点
		Integer rootCode = oDAO.getRoot(cu_organCode(request)).getCode();
		if (aDAO.findByMap(oDAO.getSonCodeList(rootCode), map, pb) == RESULT.SUCCESS) {
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.GOODS_APPLY_LIST);
	}
	
	// 单个申请单详情
		@RequestMapping(value = Url.DETAIL)
		public ModelAndView detail(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
			if (null != code) {
				ApplyInfo a = aDAO.get(code);
				if (null != a) {
					List<ApplyItem> list = aDAO.findApplyItem(code);
					a.setAdList(list);
					request.setAttribute(Parm.GOODSAPPLY, a);
				}
			}
			return new ModelAndView(Url.GOODS_APPLY_DETAIL);
		}
		

		//验证密码
		@RequestMapping(value = Url.AFFIRM)
		public void affirm(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String au = request.getParameter(Parm.AU);
			String pwd = request.getParameter(Parm.PWD);

			if (au == "" || pwd == "") {
				print(response,Msg.PARAMETER_ERROR);
				return;
			}
			UserInfo user = userDAO.getAffirm(Integer.parseInt(au), pwd);
			if (user == null) {
				print(response, Msg.ERROR);
				return;
			}
			if (!user.getPwd().equals(MD5Utils.encryptTo32(pwd))) {
				print(response, Msg.ERROR);
			} else {
				print(response, Msg.SUCCESS);
			}
		}
		
		@RequestMapping(value = Url.SAVE)
		public void save(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception{
			RESULT result = RESULT.ERROR;
			if (code != null) {
				ApplyInfo apply = aDAO.get(code);
				apply.setStatus(Sys.B_ONE);
				result = aDAO.upd(apply);
			}
			if (RESULT.SUCCESS == result) {
				print(response, Msg.SUCCESS);
			} else
				print(response, Msg.ERROR);
		}
		
		@RequestMapping(value = Url.SHIELD)
		public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String code = request.getParameter(Parm.CODE);
			if (!isNotNull(code)) {
				print(response, Msg.PARAMETER_ERROR);
				return;
			}
			ApplyInfo g = aDAO.get(Integer.parseInt(code));
			if (null == g) {
				print(response, Msg.NOT_FIND);
				return;
			}
			g.setDeleted(true);
			if (aDAO.upd(g) == RESULT.SUCCESS)
				print(response, Msg.SUCCESS);
			else
				print(response, Msg.ERROR);
		}
		
		@RequestMapping(value = Url.UN_SHIELD)
		public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String code = request.getParameter(Parm.CODE);
			if (!isNotNull(code)) {
				print(response, Msg.PARAMETER_ERROR);
				return;
			}
			ApplyInfo g = aDAO.get(Integer.parseInt(code));
			if (null == g) {
				print(response, Msg.NOT_FIND);
				return;
			}
			g.setDeleted(false);
			if (aDAO.upd(g) == RESULT.SUCCESS)
				print(response, Msg.SUCCESS);
			else
				print(response, Msg.ERROR);
		}
		
	@Autowired
	public void setaDAO(ApplyInfoDAO aDAO) {
		this.aDAO = aDAO;
	}
	@Autowired
	public void setoDAO(OrganDAO oDAO) {
		this.oDAO = oDAO;
	}
	@Autowired
	public void setUserDAO(UserInfoDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	
	
}
